package com.reader.api.controller.comment;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.reader.api.controller.BaseController;
import com.reader.api.entity.comment.CommentListRequest;
import com.reader.api.entity.comment.ReaderComment;
import com.reader.api.entity.comment.ReaderCommentPraise;
import com.reader.api.service.comment.IReaderCommentPraiseService;
import com.reader.api.service.comment.IReaderCommentService;
import com.reader.core.entity.ResultMessageInfo;
import com.reader.core.utils.ValidatorUtil;
import com.reader.core.validator.group.AddGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description   功能描述
 * @author        jiahaikun
 * @date          2018-11-09
 */
@RestController
@RequestMapping("/api/reader/comment/")
public class ReaderCommentController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(ReaderCommentController.class);

    @Autowired
    public IReaderCommentService iReaderCommentService;

    @Autowired
    public IReaderCommentPraiseService iReaderCommentPraiseService;

     /**杂志详情留言列表
     * @param commentListRequest
     * @return
     * @function 分页查询数据
     */
    @PostMapping("list")
    public ResultMessageInfo selectListPage(@RequestBody CommentListRequest commentListRequest) {
        ValidatorUtil.validateEntity(commentListRequest, AddGroup.class);//非空验证
        Integer appUserId = getUserId();
        Page<LinkedHashMap<String, Object>> records = iReaderCommentService.selectListPage(commentListRequest.getCurrentPage(),
                                                                              commentListRequest.getSize(),
                                                                              commentListRequest.getDocId(),
                                                                               appUserId);
        if(records==null||records.getSize()<=0){
            return    ResultMessageInfo.ofMessage("查不到数据");
        }

        List<LinkedHashMap<String, Object>> commentList = records.getRecords().stream()
                .collect(Collectors.groupingBy(
                        q -> q.get("commentId")+ "_"+q.get("commentPraiseCnt")+ "_"+q.get("commentPraiseState")+
                                "_"+q.get("appUserNick")+  "_"+q.get("avatar")+ "_"+q.get("comment")+ "_"+q.get("commentDt")
                                + "_"+q.get("isToTop")
                        )
                )
                .entrySet()
                .stream()
                .map(e -> {
                    LinkedHashMap<String, Object> inner = new LinkedHashMap<>();
                    List<LinkedHashMap<String, Object>> newList = e.getValue().stream().map(v -> {
                        v.remove("commentId");
                        v.remove("commentPraiseCnt");
                        v.remove("commentPraiseState");
                        v.remove("appUserNick");
                        v.remove("avatar");
                        v.remove("comment");
                        v.remove("commentDt");
                        v.remove("isToTop");
                        return v;
                    }).collect(Collectors.toList());
                    inner.put("reply", newList);
                    String[] temp = e.getKey().split("_");
                    Integer commentId = Integer.parseInt(temp[0]);
                    Integer commentPraiseCnt = Integer.parseInt(temp[1]);
                    Integer commenntPraiseState=Integer.parseInt(temp[2]);
                    String appUserNick=temp[3];
                    String avatar = temp[4];
                    String comment = temp[5];
                    String commentDt =temp[6];
                    Integer isToTop =Integer.parseInt(temp[7]);

                    inner.put("commentId",commentId);
                    inner.put("commentPraiseCnt",commentPraiseCnt);
                    inner.put("commentPraiseState",commenntPraiseState);
                    inner.put("appUserNick",appUserNick);
                    inner.put("avatar",avatar);
                    inner.put("comment",comment);
                    inner.put("commentDt",commentDt);
                    inner.put("isToTop",isToTop);
                    return inner;
                }).collect(Collectors.toList());


        records.setRecords(commentList);

        return ResultMessageInfo.ofSuccess("查询成功！", records);
    }

    /**
     * @param readerComment 实体对象
     * @return
     * @function 留言提交接口
     */
    @PostMapping("setComment")
    public ResultMessageInfo insert(@RequestBody ReaderComment readerComment) {
        ValidatorUtil.validateEntity(readerComment, AddGroup.class);//非空验证
        Integer appUserId = getUserId();
        readerComment.setDocId(readerComment.getDocId());
        readerComment.setAppUserId(appUserId);//app用户ID
        readerComment.setInsertDt(new Date());
        readerComment.setDelFlag(0);
        readerComment.setEnable(1);
        boolean resultValue = iReaderCommentService.insert(readerComment);
        if (!resultValue) {
            ResultMessageInfo.ofMessage("添加失败！");
        }
        return ResultMessageInfo.ofSuccess("添加成功！");
    }

    /**
     * @param readerCommentPraise 实体对象
     * @return
     * @function 留言点赞的接口
     */
    @PostMapping("setCommentPraise")
    public ResultMessageInfo insert(@RequestBody ReaderCommentPraise readerCommentPraise) {
        ValidatorUtil.validateEntity(readerCommentPraise, AddGroup.class);//非空验证
        Integer appUserId = getUserId();
        readerCommentPraise.setAppUserId(appUserId); //app用户ID
        readerCommentPraise.setCommentId(readerCommentPraise.getCommentId());//评论ID
        Integer praise=readerCommentPraise.getPraise();
        if(praise==1){ //1为点赞
            readerCommentPraise.setInsertDt(new Date());
            boolean resultValue = iReaderCommentPraiseService.insert(readerCommentPraise);
            if (!resultValue) {
                ResultMessageInfo.ofMessage("点赞失败！");
            }
        }else { //0为取消点赞,直接删除
            EntityWrapper<ReaderCommentPraise> ew = new EntityWrapper<ReaderCommentPraise>(readerCommentPraise);
            boolean resultValue = iReaderCommentPraiseService.delete(ew);//
            if (!resultValue) {
                ResultMessageInfo.ofMessage("点赞失败！");
            }
        }




        return ResultMessageInfo.ofSuccess("添加成功！");
    }

}


